package com.zzty.daoimpl;

import com.zzty.common.DBUtil;
import com.zzty.common.DBUtilPool;
import com.zzty.dao.CompetionDao;
import com.zzty.entity.SelfExam;
import com.zzty.entity.SelfQuestion;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class CompetionDaoImpl implements CompetionDao {
    public boolean insertQuestionId(String questionId, Integer classId,String date){
        boolean result=false;
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("insert into selfExam(questionId,classId,date) values(?,?,?)");
            pst.setString(1,questionId);
            pst.setInt(2,classId);
            pst.setString(3,date);
            Integer count=pst.executeUpdate();
            if(count==1){
                result=true;
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }



    public List getSelfExamToTable() {
        List <SelfExam> selfExams=null;
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from selfExam");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                if(selfExams==null){
                    selfExams=new ArrayList<SelfExam>();
                }
                SelfExam selfExam=new SelfExam();
                selfExam.setId(rs.getInt("Id"));
                selfExam.setDate(rs.getDate("date"));
                selfExam.setQuestionId(rs.getString("questionId"));
                selfExam.setClassId(rs.getInt("classId"));
                selfExam.setWinnerId(rs.getInt("winnerId"));
                selfExams.add(selfExam);
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return selfExams==null?null:selfExams;
    }

    public boolean checkDate(String date) {
        boolean result=true;
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from selfExam");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if(date.equals(sdf.format(rs.getDate("date")))){
                    result=false;
                }
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public String getQuestionIdByDate(String date) {
        String questionId=null;
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from selfExam where Date=?");
            pst.setString(1,date);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                questionId=rs.getString("questionId");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return questionId==null?null:questionId;
    }
}

